﻿using ReadWriteCsv;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace StockAnomalyDetector
{
    // Fly 是指那些违背市场规律的捣乱者，市场规律体现在对SB的权重的比值所决定的市场规律的违背
    // 一天有有十分钟以上的违背的，记录下来
    class Fly
    {
        public double preAvLevel;
        public double avLevel;

        private bool even; // 每两个区间分析一次
        private double countB;
        private double countS;
        private double ProbabilityOfRising;

        private List<int> STime;
        private List<int> BTime;
        private List<double> Sp;
        private List<double> Bp;


        public static List<string> fly = new List<string>() { "stockCode,date,startTime,endTime,probability" };
        public static CsvFileWriter writer = new CsvFileWriter(@"D:\股票数据\" + "Fly" + ".csv");
        public Fly()
        {
            preAvLevel = 0;
            countB = 0;
            countS = 0;
            ProbabilityOfRising = 0.5;
            avLevel = 0;
            even = false;
            STime = new List<int>();
            BTime = new List<int>();
            Sp = new List<double>();
            Bp = new List<double>();
        }

        public void update(char BorS, int quantity)
        {
            if (BorS == 'B')
                countB+= quantity;
            else
                countS+= quantity;
        }

        public void analyzeSlice(Clock endTime)
        {
            if (even) 
                ProbabilityOfRising = countB / (countB + countS);
            //if(ProbabilityOfRising == 0)
            //{
            //    Console.WriteLine("B: {0}",countB);
            //    Console.WriteLine("S: {0}",countS);
            //}
            if (ProbabilityOfRising > 0.8 && avLevel / preAvLevel < 0.995)
            {
                BTime.Add(endTime.toInt32());
                Bp.Add(ProbabilityOfRising);
            }
            if (ProbabilityOfRising < 0.2 && avLevel / preAvLevel > 1.005)
            {
                BTime.Add(endTime.toInt32());
                Bp.Add(ProbabilityOfRising);
            }

            even = !even;
        }

        public void analyzeRecord(string sourceName)
        {
            if (STime.Count > 0 || BTime.Count > 0)
            {
                FlyRecord r;
               for(int i=0; i<STime.Count;i++)
                {
                    r = new FlyRecord
                    {
                        stockcode = Convert.ToString(Regex.Match(sourceName, @"[A-Z]{2}\d{6}")),
                        date = Convert.ToString(Regex.Match(sourceName, @"\d{8}")),
                        startTime = STime[i],
                        endTime = STime[i],
                        p = Sp[i]
                    };
                    fly.Add(r.ToString());
                    Console.WriteLine("sFly: "+r.ToString());
                }
               for(int i =0; i<BTime.Count;i++)
                {
                    r = new FlyRecord
                    {
                        stockcode = Convert.ToString(Regex.Match(sourceName, @"[A-Z]{2}\d{6}")),
                        date = Convert.ToString(Regex.Match(sourceName, @"\d{8}")),
                        startTime = BTime[i],
                        endTime = BTime[i],
                        p = Bp[i]
                    };
                    fly.Add(r.ToString());
                    Console.WriteLine("bFly: " + r.ToString());
                }
            }

        }

        public void reset()
        {
            // 这个时候这个概率应该被使用了
            if (ProbabilityOfRising != 0.5)
            {
                countB = countS = 0;
                ProbabilityOfRising = 0.5;
            }
        }

    }
    class FlyRecord : Record
    {
        public double p;

        public override string ToString()
        {
            return string.Format("{0},{1: 0.00}", base.ToString(), p);
        }
    }

}
